﻿using System.Windows.Data;
using System;
using System.Globalization;
using System.Windows.Media.Imaging;
using System.IO;

namespace ProxyViewModel.Samples.TodoList.Views {
	/// <summary>
	/// This converter gets image depending on weather task is completed or not.
	/// </summary>
	public class StatusImageConverter : IValueConverter {
		#region IValueConverter Members

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
			if ((bool)value) {
				return LoadImage("Icons/mark.png");
			}
			else {
				return LoadImage("Icons/cross.png");
			}
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
			throw new InvalidOperationException();
		}

		#endregion

		private BitmapImage LoadImage(string path) {
			BitmapImage bmp = new BitmapImage();
			bmp.BeginInit();
			bmp.DecodePixelWidth = 8;
			bmp.CacheOption = BitmapCacheOption.OnLoad;
			byte[] bytes = File.ReadAllBytes(path);
			MemoryStream strm = new MemoryStream(bytes);
			bmp.StreamSource = strm;
			bmp.EndInit();
			return bmp;
		}
	}
}
